package 优先算法.模拟.外观数列;

/**
 *https://leetcode.cn/problems/count-and-say/description/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        String rel = so.countAndSay(4);
        System.out.println(rel);
    }
}
class Solution {
    //模拟+双指针
    public String countAndSay(int n) {

        String rel = "1";
        for(int i = 1; i < n; i++){
            StringBuilder temp = new StringBuilder();
            int len = rel.length();
            for(int left = 0,right = 0;right < len; ){
                while(right < len && rel.charAt(right)==rel.charAt(left)){
                    right++;
                }
                temp.append(Integer.toString(right-left));
                temp.append(rel.charAt(left));
                left = right;
            }
            rel = temp.toString();
        }
        return rel;
    }
}